var mysql = require("mysql")
// var fs = require("fs")
var express = require("express") //使用commondjs的形式导入的依赖
// import express from "express"  //使用es6的形式导入的依赖
var app = express()
var Mock = require('mockjs')  //模拟测试

// Vue elementUi项目专用接口
app.get("/api/login", function (req, res) {
    console.log(req.query)
    res.setHeader("Access-Control-Allow-Origin", "*")
    if (req.query.userName == "admin" && req.query.pwd == "123") {
        res.send("success")
    } else {
        res.send("falie")

    }

})
// Vue elementUi项目专用接口 读取数据 t_user_list
app.get("/uiData", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "linjiashop"
    })

    connection.connect();

    connection.query('SELECT*FROM t_user_list', function (err, result) {
        res.send(result)
        // console.log("666")
    })
})
// 查询数据
app.get("/searchtxt", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "linjiashop"
    })

    connection.connect();

    connection.query('SELECT * FROM `t_wenzhangs`', function (err, result) {
        res.send(result)
        if (err) {
            console.log(err)
            console.log("666")

        }
    })
})


// 修改数据
app.get("/change", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "linjiashop"
    })
    // console.log(req.query)
    connection.connect();
    var updateSql = "UPDATE `t_user_list`  SET  mobile=? , consignee_address=? WHERE  userName=?";
    // var arr = [1234, "红米8"];
    console.log(req.query)
    var arr = [req.query.mobile, req.query.conignee_address, req.query.userName];
    connection.query(updateSql, arr, function (error, result) {
        console.log(updateSql)

        if (error) {
            console.log(error.message)
            return
        } else {
            console.log(req.query.userName)
        }
        res.send(result)
        console.log(result.affectedRows)
    })
})
// 删除数据
app.get("/delete", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "linjiashop"
    })

    connection.connect((err) => {
        console.log(err)

        if (err) {
            console.log(err)
        } else {
            console.log("数据库连接成")
        }
    });
    var delSql = "DELETE FROM `t_user_list` WHERE id=?";
    console.log(req.query)
    var arr = req.query.id;
    connection.query(delSql, arr, function (err, result) {
        res.send(result)
        console.log(req.query.id)
        console.log(result.affectedRows)
    })
})
// 增加数据
app.get("/creat", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "linjiashop"
    })

    connection.connect();
    var creatSql = "INSERT INTO `t_user_list` (userName,mobile,consignee_address,modify_time) VALUES (?,?,?,now())";
    console.log(req.query)
    var arr1 = [req.query.userName, req.query.mobile, req.query.conignee_address, req.query.modify_time]
    if (req.query.userName == '' || req.query.userName == null || req.query.userName == undefined) {
        return
    } else {

    }
    connection.query(creatSql, arr1, function (err, result) {
        // connection.query("INSERT INTO `t_shop_goods`(NAME,price,create_time) VALUES ('大液晶电视',8888,'020-01-27 11:44:06')",function (err, result) {
        if (err) {
            console.log(err)
        }
        res.send(result)
        // console.log(result.affectedRows)
    })
})
// 条件搜索
app.get("/seachInfo", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "linjiashop"
    })
    connection.connect();
    var arr1 = [];


    var seachSql = "SELECT userName,mobile,consignee_address,id FROM t_user_list  ";
    console.log(req.query)
    // var arr1 = [req.query.id,req.query.userName,req.query.consignee_address,req.query.mobile];
    // 如果名字 手机号为空 null undeFined  则只搜索id
    if ((req.query.userName == '' || req.query.userName == null || req.query.userName == undefined) &&
        (req.query.mobile == '' || req.query.mobile == null || req.query.mobile == undefined)) {
        seachSql += "WHERE id=? "
        arr1 = [req.query.id];
        console.log(seachSql)
        console.log(arr1)
        console.log("999")
    }
    else if ((req.query.userName != '' || req.query.userName != null || req.query.userName != undefined) &&
        (req.query.mobile == '' || req.query.mobile == null || req.query.mobile == undefined) &&
        (req.query.id == '' || req.query.id == null || req.query.id == undefined)
    ) {

        arr1 = [req.query.userName]
        seachSql += "WHERE userName LIKE '%' ? '%'";
        console.log(arr1)
        console.log("456")
    }

    connection.query(seachSql, arr1, function (err, result) {
        if (err) {
            console.log(err)
            console.log(seachSql)
            console.log(arr1)
            console.log("出错了")
        }
        // console.log(result)
        res.send(result)
        //    return result
    })
})
// 修改文章信息
app.get("/changeTxt", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "linjiashop"
    })
    // console.log(req.query)
    connection.connect();
    var updateSql = " UPDATE `t_wenzhang`  SET  titles=? WHERE  id=?";
    // var arr = [1234, "红米8"];
    // console.log(req.query)
    var arr = [req.query.titles, req.query.id];
    console.log(req.query)
    console.log(req.query)

    connection.query(updateSql, arr, function (error, result) {
        console.log(updateSql)

        if (error) {
            console.log(error.message)
            return
        } else {
            console.log(req.query.titles)
            console.log("11")
        }
        res.send(result)
        console.log(result.affectedRows)
    })
})
app.get(
    app.get("/api/del", function (req, res) {
        res.setHeader("Access-Control-Allow-Origin", "*")
        var connection = mysql.createConnection({
            host: "localhost",
            user: "root",
            password: "root",
            database: "linjiashop"
        })

        connection.connect();
        var delSql = "DELETE FROM `t_wenzhangs` WHERE id =?";
        console.log(req.query)
        var del = req.query.id;
        console.log(del)
        connection.query(delSql, del, function (err, result) {
          
            if (err) {
                console.log(err)
                console.log(del)
            }
            res.send(result)
            // console.log(req.query.id)
            console.log(result)

        })
    })
)

app.listen(3001, function () {
    console.log("server is runing")
})




















